package ms.core.gateway.dao;

import java.util.ArrayList;
import java.util.List;

import ms.core.gateway.bean.SysParamBean;
import ms.core.gateway.response.SysParamVO;
import ms.core.tool.StrTool;

public class SysParamDao extends BaseDao{
	
	public SysParamDao() { super("sys_param"); }
	
	/**
	 * 更新参数值
	 * 
	 * @param code
	 * @param value
	 * @return
	 */
	public int updateParam(String code, String value) {
		String sql = String.format("update %s set pvalue=? where pcode=?", table);
		return db.executeSql(sql, new Object[] {value, code});
	}
	
	/**
	 * 加载所有参数
	 * 
	 * @return
	 */
	public List<SysParamBean> loadParams(){
		String sql = String.format("select * from %s", table);
		return db.queryForBeans(sql, SysParamBean.class);
	}

	/**
	 * 按参数类型查参数
	 * 
	 * @param pcode 参数名
	 * @param ptype 参数类型
	 * @return
	 */
	public List<SysParamVO> queryParam(String pcode, int ptype){
		List<Object> params = new ArrayList<>();
		String sql = String.format("select * from %s where 1>0", table);
		if (!StrTool.isBlank(pcode)) {
			sql += " and pcode=?";
			params.add(pcode);
		}
		if (ptype>=0) {
			sql += " and ptype=?";
			params.add(ptype);
		}
		return db.queryForBeans(sql, SysParamVO.class, params);
	}
}
